﻿namespace CKFinder.Settings
{
    using CKFinder.Connector;
    using System;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.UI;

    public class ConfigFile : UserControl
    {
        private AccessControlManager _AccessControl = new AccessControlManager();
        private static ConfigFile _Current;
        private string[] _HideFiles;
        private string[] _HideFolders;
        private CKFinder.Settings.Images _Images = new CKFinder.Settings.Images();
        private ResourceTypeManager _ResourceType = new ResourceTypeManager();
        private CKFinder.Settings.Thumbnails _Thumbnails = new CKFinder.Settings.Thumbnails();
        public string BaseDir;
        public string BaseUrl;
        public bool CheckSizeAfterScaling;
        public string DefaultResourceTypes;
        public bool ForceSingleExtension;
        internal Regex HideFilesRegex;
        internal Regex HideFoldersRegex;
        public string[] HtmlExtensions;
        public string LicenseKey;
        public string LicenseName;
        public string RoleSessionVar;
        public bool SecureImageUploads;

        public ConfigFile()
        {
            this.HideFolders = new string[0];
            this.HideFiles = new string[0];
            this.LicenseName = "";
            this.LicenseKey = "";
            this.BaseUrl = "/ckfinder/userfiles/";
            this.BaseDir = "";
            this.ForceSingleExtension = true;
            this.CheckSizeAfterScaling = true;
            this.DefaultResourceTypes = "";
            this.HtmlExtensions = new string[0];
            this.RoleSessionVar = "";
        }

        private static Regex BuildHideRegex(string[] entries)
        {
            string str = Regex.Escape(string.Join("<PIP>", entries).Replace("*", "<AST>").Replace("?", "<QMK>")).Replace("<AST>", ".*").Replace("<QMK>", ".").Replace("<PIP>", "|");
            return new Regex("^(?:" + str + ")$", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
        }

        public virtual bool CheckAuthentication()
        {
            return false;
        }

        public virtual void SetConfig()
        {
        }

        internal void SetCurrent()
        {
            _Current = this;
        }

        public AccessControlManager AccessControl
        {
            get
            {
                return this._AccessControl;
            }
        }

        internal static ConfigFile Current
        {
            get
            {
                if (_Current == null)
                {
                    CKFinder.Connector.Connector handler = HttpContext.Current.Handler as CKFinder.Connector.Connector;
                    if (handler == null)
                    {
                        _Current = new ConfigFile();
                    }
                    else
                    {
                        _Current = handler.ConfigFile;
                    }
                }
                return _Current;
            }
        }

        public string[] HideFiles
        {
            get
            {
                return this._HideFiles;
            }
            set
            {
                this._HideFiles = value;
                this.HideFilesRegex = BuildHideRegex(value);
            }
        }

        public string[] HideFolders
        {
            get
            {
                return this._HideFolders;
            }
            set
            {
                this._HideFolders = value;
                this.HideFoldersRegex = BuildHideRegex(value);
            }
        }

        public CKFinder.Settings.Images Images
        {
            get
            {
                return this._Images;
            }
        }

        public ResourceTypeManager ResourceType
        {
            get
            {
                return this._ResourceType;
            }
        }

        public CKFinder.Settings.Thumbnails Thumbnails
        {
            get
            {
                return this._Thumbnails;
            }
        }
    }
}

